package ma.ensao.egicom.market.client.controller;

import java.util.ArrayList;
import java.util.List;

import ma.ensao.egicom.core.dto.CategoryDto;
import ma.ensao.egicom.core.dto.ProductDto;
import ma.ensao.egicom.core.services.interfaces.CategoryService;
import ma.ensao.egicom.core.services.interfaces.ProductService;
import ma.ensao.egicom.market.client.model.CategoryModel;
import ma.ensao.egicom.market.client.model.ProductModel;

import org.scub.foundation.framework.base.mapping.util.MapperDozerBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {

    /** URL de la page successView */
    @SuppressWarnings("unused")
    private String successView;

    /** Mapper Dozer */
    private MapperDozerBean mapperDozerBean;



    private ProductService productService;

    private CategoryService categoryService;


    /** URL de la page homeView */
    private String homeView;



    @RequestMapping(value = "home.do")
    public String initView( final Model model) {

        //Get Last Products
        List<ProductDto> listProductsDto = productService.getNewProducts();
        List<ProductModel> listLastProductsModel = mapperDozerBean.mapList(listProductsDto, ProductModel.class);
        for (int i = 0; i < listLastProductsModel.size(); i++) {
            listLastProductsModel.get(i).setCategory(listProductsDto.get(i).getCategory().getName());
            listLastProductsModel.get(i).setSupplier(listProductsDto.get(i).getSupplier().getFirstname());
        }

        //Get Categories Name
        List<CategoryDto> listCategorie = categoryService.getAllCategories();
        List<ProductModel> listCatModel = mapperDozerBean.mapList(listCategorie, CategoryModel.class);
        
        //Get popular Products
//        List<ProductDto> listPopularProductsDto = new ArrayList<ProductDto>();
//        List<ProductModel> listPopularProductsModel = mapperDozerBean.mapList(listPopularProductsDto, ProductModel.class);

        model.addAttribute("listLastProducts", listLastProductsModel);
//        model.addAttribute("listPopularProducts", listPopularProductsModel);
        model.addAttribute("category", listCatModel);
        return homeView;
    }

    public void setMapperDozerBean(MapperDozerBean mapperDozerBean) {
        this.mapperDozerBean = mapperDozerBean;
    }

    public void setSuccessView(String successView) {
        this.successView = successView;
    }


    public void setHomeView(String homeView) {
        this.homeView = homeView;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

}

